<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class AuthService extends CI_Controller
{

	//Doctrine EntityManager
	public $em;

	function __construct()
	{
 		parent::__construct();

  		//Not required if you autoload the library
 		$this->load->library('doctrine');

  		//Instantiate a Doctrine Entity Manager
 		$this->em = $this->doctrine->em;
 	}
	
	public function index()
	{
		die("index");
		$user = new models\User;
		$user->setUsername('Joseph');
		$user->setPassword('Password');		
		$this->em->persist($user);
		$this->em->flush(); 
	}
	
	public function login(){
		$user = $this->input->post('user');
		$pass = $this->input->post('password');
		if ($user != "" && $pass != ""){
			$criteria = array('userId' => $user);
			$user = $this->em->getRepository('\models\entities\User')->findOneBy($criteria);
			if ($user != null){
				if ($user->getPassword() == $pass){
					header('location: '.base_url().'Dashboard/');				
				} else {
					echo "pass no match";
				}
			} else {
				echo "no user";
			}
		} else {
			echo "input tidak boleh kosong";
		}
		
		
	}

	public function testModelNode($function="", $param=""){
		switch ($function) {
			case 'add':
				$node = new models\entities\Node;
				
				$criteria = array('id' => 1);
				$tag = $this->em->getRepository('\models\entities\Tag')->findOneBy($criteria);
				$node->setMainTag($tag);

				$criteria = array('id' => 1);
				$indexNode = $this->em->getRepository('\models\entities\IndexNode')->findOneBy($criteria);
				$node->setIndexNode($indexNode);
				echo "<pre>"; print_r($node);
				$this->em->persist($node);
				$this->em->flush(); 
				
				echo "<br><a href='".base_url()."TestDoctrine/testModelNode/'> Back </a>";
				break;
			case 'delete':
				if ($param != "" && (int)$param > 0){
					$criteria = array('id' => $param);
					$node = $this->em->getRepository('\models\entities\Node')->findOneBy($criteria);
					echo "<pre>";var_dump($node);
					$this->em->remove($node);
					$this->em->flush();					
				}			
				echo "<br><a href='".base_url()."TestDoctrine/testModelNode/'> Back </a>";
				break;
			case 'update':
				if ($param != "" && (int)$param > 0){
					$criteria = array('id' => $param);
					$node = $this->em->getRepository('\models\entities\Node')->findOneBy($criteria);
					
					$criteria = array('id' => 2);
					$indexNode = $this->em->getRepository('\models\entities\IndexNode')->findOneBy($criteria);
					$node->setIndexNode($indexNode);
					echo "<pre>";
					print_r($node);
					$this->em->persist($node);
					$this->em->flush();					
				}
				echo "<br><a href='".base_url()."TestDoctrine/testModelNode/'> Back </a>";
				break;	
			default:
				// view
				$nodes = $this->em->getRepository('\models\entities\Node')->findAll();
				echo "<h1>Test :: View All Node</h2>";
				foreach ($nodes as $node){
					echo "<br>".$node->getId(). " - " . $node->getMainTag(). " - ". $node->getIndexNode() . " -> " . "<a href='".base_url()."TestDoctrine/testModelNode/delete/".$node->getId()."'>Delete</a> | <a href='".base_url()."TestDoctrine/testModelNode/update/".$node->getId()."'>Update</a>";
				}				
				
				echo "<br><a href='".base_url()."TestDoctrine/testModelNode/add'> Test Add </a>"; 
				break;
		}
	}
	
	public function testModelTag($function=""){
		switch ($function) {
			case 'vadd':
				$data['actionUrl'] = base_url()."TestDoctrine/testModelTag/add";
				$this->load->view('frmTag', $data);				
				break;
			case 'add':
				// validation
				var_dump($_POST);
				$tag = new models\entities\Tag;
				$tag->setLocation($_POST['location']);
				$tag->setNama($_POST['nama']);
				$tag->setProperties($_POST['properties']);
				$this->em->persist($tag);
				$this->em->flush(); 
				echo "<br><a href='".base_url()."TestDoctrine/testModelTag/'> Back </a>";
				break;	
			case 'delete':
				break;
			case 'update':
				break;	
			default:
				// view
				$tags = $this->em->getRepository('\models\entities\Tag')->findAll();
				echo "<h1>Test :: View All Tag</h2>";
				foreach ($tags as $tag){
					echo "<br>".$tag->getId(). " - " . $tag->getNama(). " - ". $tag->getLocation() . " -> " . "<a href='".base_url()."TestDoctrine/testModelTag/delete/".$tag->getId()."'>Delete</a> | <a href='".base_url()."TestDoctrine/testModelTag/update/".$tag->getId()."'>Update</a>";
				}	
				
				echo "<br><a href='".base_url()."TestDoctrine/testModelTag/vadd'> Test Add </a>"; 
				break;
		}
	}
	
	public function testModelRel($function=""){
		switch ($function) {
			case 'add':				
				break;
			case 'delete':
				break;
			case 'update':
				break;	
			default:
				// view
				$rels = $this->em->getRepository('\models\entities\Rel')->findAll();
				echo "<h1>Test :: View All Rel</h2>";
				var_dump($rels);
				break;
		}
	}
	
	public function testModelRelTag($function=""){
		switch ($function) {
			case 'add':				
				break;
			case 'delete':
				break;
			case 'update':
				break;	
			default:
				// view
				$reltags = $this->em->getRepository('\models\entities\RelTag')->findAll();
				echo "<h1>Test :: View All RelTag</h2>";
				var_dump($reltags);
				break;
		}
	}
	
	public function testModelTrayek($function=""){
		switch ($function) {
			case 'add':				
				break;
			case 'delete':
				break;
			case 'update':
				break;	
			default:
				// view
				$trayeks = $this->em->getRepository('\models\entities\Trayek')->findAll();
				echo "<h1>Test :: View All Trayek </h2>";
				var_dump($trayeks);
				break;
		}
	}
	
	public function testModelTrayekGroup($function=""){
		switch ($function) {
			case 'add':				
				break;
			case 'delete':
				break;
			case 'update':
				break;	
			default:
				// view
				$trayekGroups = $this->em->getRepository('\models\entities\TrayekGroup')->findAll();
				echo "<h1>Test :: View All Trayek Group </h2>";
				var_dump($trayekGroups);
				break;
		}
	}
	
	public function testModelTrayekRel($function=""){
		switch ($function) {
			case 'add':				
				break;
			case 'delete':
				break;
			case 'update':
				break;	
			default:
				// view
				$trayekRels = $this->em->getRepository('\models\entities\TrayekRel')->findAll();
				echo "<h1>Test :: View All Trayek Rel </h2>";
				var_dump($trayekRels);
				break;
		}
	}
	
}